package top.yokey.shopnc.activity.goods;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.squareup.otto.Subscribe;
import top.yokey.shopnc.R;
import top.yokey.shopnc.adapter.BaseFragmentAdapter;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseBusClient;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.base.event.GoodsAreaEvent;
import top.yokey.base.event.GoodsBeanEvent;
import top.yokey.base.event.GoodsEvaluateEvent;
import top.yokey.base.event.GoodsGoneEvent;
import top.yokey.base.event.GoodsShowEvent;
import top.yokey.base.event.GoodsIdEvent;
import top.yokey.base.model.GoodsModel;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MapStory
 * @ qq 1002285057
 * @ Project https://gitee.com/MapStory/ShopNc-Android
 */

@ContentView(R.layout.activity_goods_goods)
public class GoodsActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;
    @ViewInject(R.id.goodsTextView)
    private AppCompatTextView goodsTextView;
    @ViewInject(R.id.detailedTextView)
    private AppCompatTextView detailedTextView;
    @ViewInject(R.id.evaluateTextView)
    private AppCompatTextView evaluateTextView;
    private AppCompatTextView[] navigationTextView;

    @ViewInject(R.id.mainViewPager)
    private ViewPager mainViewPager;

    private String goodsIdString;
    private boolean isShowBoolean;

    @Override
    public void initData() {

        goodsIdString = getIntent().getStringExtra(BaseConstant.DATA_ID);

        if (TextUtils.isEmpty(goodsIdString)) {
            BaseToast.get().showDataError();
            BaseApplication.get().finish(getActivity());
        }

        navigationTextView = new AppCompatTextView[3];
        navigationTextView[0] = goodsTextView;
        navigationTextView[1] = detailedTextView;
        navigationTextView[2] = evaluateTextView;

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new GoodsFragment());
        fragmentList.add(new DetailedFragment());
        fragmentList.add(new EvaluateFragment());

        mainViewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList));
        mainViewPager.setOffscreenPageLimit(navigationTextView.length);
        isShowBoolean = false;

        setToolbar(mainToolbar, "");

        updateNavigation(0);
        getGoodsInfo();

    }

    @Override
    public void initEven() {

        for (int i = 0; i < navigationTextView.length; i++) {
            final int position = i;
            navigationTextView[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateNavigation(position);
                }
            });
        }

        mainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateNavigation(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onReturn() {

        if (mainViewPager.getCurrentItem() != 0) {
            mainViewPager.setCurrentItem(0);
            return;
        }

        if (isShowBoolean) {
            BaseBusClient.get().post(new GoodsGoneEvent(true));
            return;
        }

        super.onReturn();

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            onReturn();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onActivityResult(int req, int res, Intent intent) {
        super.onActivityResult(req, res, intent);
        if (res == RESULT_OK) {
            switch (req) {
                case BaseConstant.CODE_AREA:
                    BaseBusClient.get().post(new GoodsAreaEvent(intent.getStringExtra("area_id"), intent.getStringExtra("area_info")));
                    break;
                default:
                    break;
            }
        }
    }

    //自定义方法

    private void getGoodsInfo() {

        BaseSnackBar.get().showHandler(mainLinearLayout);

        GoodsModel.get().goodsDetailed(goodsIdString, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                BaseBusClient.get().post(new GoodsBeanEvent(baseBean));
            }

            @Override
            public void onFailure(String reason) {
                if (reason.equals("商品不存在")) {
                    BaseToast.get().show(reason);
                    BaseApplication.get().finish(getActivity());
                } else {
                    BaseSnackBar.get().show(mainLinearLayout, reason);
                }
            }
        });

    }

    private void updateNavigation(int position) {

        for (AppCompatTextView appCompatTextView : navigationTextView) {
            appCompatTextView.setTextColor(BaseApplication.get().getColors(R.color.grey));
        }

        navigationTextView[position].setTextColor(BaseApplication.get().getColors(R.color.primary));
        mainViewPager.setCurrentItem(position);

    }

    @Subscribe
    @SuppressWarnings("unused")
    public void onGoodsIdEvent(GoodsIdEvent event) {

        goodsIdString = event.getGoodsId();
        getGoodsInfo();

    }

    @Subscribe
    @SuppressWarnings("unused")
    public void onGoodsShowEvent(GoodsShowEvent event) {

        isShowBoolean = event.isShow();

    }

    @Subscribe
    @SuppressWarnings("unused")
    public void onGoodsEvaluateEvent(GoodsEvaluateEvent event) {

        mainViewPager.setCurrentItem(2);

    }

}
